from google.appengine.ext import db

class Query(db.Model):
  search_string = db.StringProperty(required=True)
  images = db.StringListProperty(required=True)
  date = db.DateTimeProperty(auto_now_add=True)
  
def AddQuery(ss, ilist):
  query = GetQuery(ss)
  if query is None:
    query = Query(search_string=ss, images=["http://re.flect.net/"+i.img['src'] for i in ilist])
    query.put()

  return query

def GetQuery(ss):
  query = Query.gql('WHERE search_string = :1', ss)
  results = query.fetch(1)

  if len(results) == 1:
    return results[0]
  else:
    return None

def GetQueryImages(ss):
  item = GetQuery(ss)
  if item is None:
    return None

  return item.images